
<template>
    <div>
        <Title :title="title"></Title>
        <div ref="right1" style="height: 150px; margin-top: 20px;"></div>
    </div>
</template>
<script>
import { DualAxes } from '@antv/g2plot';
import { get } from '@/utils/request';
import moment from 'moment';
import { mapState } from 'vuex';

export default {
    data() {
        return {
            title: '实时湿度曲线',
            params: {
                page: 1,
                pageSize: 100,
            },
            TData: [],
            line:null,
        }
    },
    computed: {
        ...mapState(['ed']),
    },
    watch:{
        ed:{
            handler(n,o){
                // 根据设备信息重新查询数据 渲染图表
                this.params.engineer_id = n.engineer_id;
                this.params.device_id = n.device_id;
                this.getProcessData();
                // 根据最新获取的数据重新渲染图表
                this.dualAxes.changeData([this.TData,this.TData]);
            },
            // 深度侦听
            deep:true,
        }
    },
    methods: {
        loadLine() {
            this.dualAxes = new DualAxes(this.$refs.right1, {
                data:[this.TData,this.TData],
                xField: 'insert_time',
                yField: ['temperature', 'humidity'],
                xAxis: {
                    label: {
                        style: {
                            fill: "white",
                            fontSize: 12,
                        },
                    },
                },
                yAxis: [
                    {
                        label: {
                            style: {
                                fill: "white",
                                fontSize: 13,
                            },
                        },
                    },
                    {
                        label: {
                            style: {
                                fill: "white",
                                fontSize: 13,
                            },
                        },
                    },
                ],
                geometryOptions: [
                    {
                        geometry: 'line',
                        color: '#5B8FF9',
                        name: 'temperature',
                    },
                    {
                        geometry: 'line',
                        color: '#5AD8A6',
                        name: 'humidity',

                    },
                ],
                legend: {
                    position: 'top',
                },
                tooltip: {
                    showMarkers: true,
                    domStyles: {
                        "g2-tooltip": {
                            background: "#147",
                            color: "white",
                            fontSize: 15,
                        },
                    },
                },
                
            });

            this.dualAxes.render();
        },
        async getProcessData() {
            let res = await get('/dashboard/pageQueryTodayData', this.params);
            // console.log(res, "获取响应");
            let temp = [];
            res.data.list.forEach((item, index, arr) => {
                // 由于刻度混乱 将第一条数据值设置为15.4
                arr[0].temperature = 15.4;
                arr[0].humidity =43;
                item.insert_time = moment(item.insert_time).format('HH:mm');
                // 由于查询数据密度密集 需要将数据处理 每隔10条数据取一条数据
                if (index % 10 == 0) {
                    temp.push(arr[index])
                }
            });
            this.TData =temp;
        },
    },

    async mounted() {
        await this.getProcessData();
        this.loadLine();
    },
}
</script>
<style lang="less" scoped></style>